package com.akaovi.exp;

import java.io.*;

/**
 * @author akaovi
 * @version 1.0
 * @description
 * @date 2022/11/14 15:23
 * @since 2022/11/14 15:23
 */
public class Four implements Interf, Serializable{

    private Integer bh;

    private String xm;

    private Integer nl;

    public Four() {
    }

    public Four(Integer bh, String xm, Integer nl) {
        this.bh = bh;
        this.xm = xm;
        this.nl = nl;
    }

    public Integer getBh() {
        return bh;
    }

    public void setBh(Integer bh) {
        this.bh = bh;
    }

    public String getXm() {
        return xm;
    }

    public void setXm(String xm) {
        this.xm = xm;
    }

    public Integer getNl() {
        return nl;
    }

    public void setNl(Integer nl) {
        this.nl = nl;
    }

    @Override
    public String toString() {
        return "Four{" +
                "bh=" + bh +
                ", xm='" + xm + '\'' +
                ", nl=" + nl +
                '}';
    }

    @Override
    public void save() {
        ObjectOutputStream oos = null;
        File file = new File("D:\\javaProject\\javaExp\\exp8\\src\\main\\resources\\对象流保存.txt");

        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(this);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void display(String path) {
        ObjectInputStream ois = null;
        File file = new File(path);
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            Four obj = (Four) ois.readObject();
            System.out.println(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Four zhangsan = new Four(1, "张三", 18);
        zhangsan.save();
        zhangsan.display("D:\\javaProject\\javaExp\\exp8\\src\\main\\resources\\对象流保存.txt");
    }
}
